/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class ClearMemoryForm extends BasicForm implements MenuList.MenuListSelection
{
    public static final int OK_COMMAND_ID = 0;
    
    private static final int CLEAR_ALL_ID = 0;
    private static final int CLEAR_ORDERS_ID = 1;
    private static final int CLEAR_MESSAGES_ID = 2;
    private static final int CLEAR_POSITIONS_ID = 3;
    
    private MenuList items;
    
    private String messageText;
    
    public ClearMemoryForm()
    {
        super("clear_mem");
        
        messageText = (new Label("was_cleared")).getText();
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        //Заполняем меню
        items = new MenuList();
        items.addItem(new CellItem(CLEAR_ALL_ID ,"clear_all"));
        items.addItem(new CellItem(CLEAR_ORDERS_ID ,"clear_orders"));
        items.addItem(new CellItem(CLEAR_MESSAGES_ID ,"clear_messages"));
        items.addItem(new CellItem(CLEAR_POSITIONS_ID ,"clear_positions"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void itemSelected(int id)
    {
        String text = "";
        if (id == CLEAR_ALL_ID)
        {
            IMAPMIDlet.getMidlet().getOrderManager().deleteAll();
            IMAPMIDlet.getMidlet().getMessageManager().deleteAll();
            IMAPMIDlet.getMidlet().getPositionManager().deleteAll();

            text = (new Label("clear_orders")).getText() + ", " + (new Label("clear_messages")).getText() +
                    ", " + (new Label("clear_positions")).getText() + " " + messageText;
        }
        else if (id == CLEAR_ORDERS_ID)
        {
            IMAPMIDlet.getMidlet().getOrderManager().deleteAll();
            
            text = (new Label("clear_orders")).getText() + " " + messageText;
        }
        else if (id == CLEAR_MESSAGES_ID)
        {
            IMAPMIDlet.getMidlet().getMessageManager().deleteAll();
            
            text = (new Label("clear_messages")).getText() + " " + messageText;
        }
        else if (id == CLEAR_POSITIONS_ID)
        {
            IMAPMIDlet.getMidlet().getPositionManager().deleteAll();
            
            text = (new Label("clear_positions")).getText() + " " + messageText;
        }
        
        showMessage(text);
    }
    
    private void showMessage(String text)
    {
        ScreenEngine.getEngine().showMessageBox("message", text, 0);
    }
}
